<?php
/**
 * Created by PhpStorm.
 * User: ciwei
 * Date: 2017/12/4
 * Time: 8:40
 */
namespace app\index\model;

use \think\Db;
use \think\Loader;
use \think\Model;

class Question extends Model
{
    protected static function init()
    {
        self::event('before_insert', function ($question) {
            $user = Loader::model('User');
            $theUser = $user->where('uid',session('uid'))->find();
            if($theUser['kiss']<$question['kiss']){
                exit(json_encode(['error'=>1,'info'=>'飞吻不够,请赚够飞吻再来发布']));
            }
        });
        self::event('after_insert', function ($question) {
            $user = Loader::model('User');
            $user->where('uid',session('uid'))->setDec('kiss',$question['kiss']);

        });
    }
    public function addQuestion($data)
    {
        if ($data['cid'] == 1){
            $data['desc'] = json_encode($data['desc']);
            $data['status'] = 0;
        }else{
            unset($data['desc']);
        }
        unset($data['file']);
        unset($data['vercode']);
        $data['ctime'] = time();
        $data['uid'] = session('uid');
        $res = Question::save($data);
        if ($res){
            session('nickname',$res['nickname']);
            exit(json_encode(['error'=>0,'info'=>'发布成功','url'=>url('index/question/index',['qid'=>$this->qid])]));
        }else{
            exit(json_encode(['error'=>1,'info'=>'发布失败']));
        }
    }
    public function getQuestion($qid)
    {
        $data = Db::table('question')
            ->alias('q')
            ->join('user u','q.uid = u.uid')
            ->join('cate c','q.cid = c.cid')
            ->field('u.uid,u.nickname,u.face,u.role,q.qid,q.title,c.cid,c.name,q.status,q.is_jing,q.content,q.desc,q.ctime,q.kiss,q.view_num,q.answer_num,u.role')
            ->order('qid desc')
            ->where('qid',$qid)
            ->find();
        return $data;

    }
    public function indexHot(){
        $data = Db::table('question')
            ->alias('q')
            ->field('q.qid,q.title,q.answer_num')
            ->order('answer_num desc')
            ->limit(15)
            ->where('q.ctime','>',time()-604800)
            ->select();
        return $data;
    }
    public function getAll()
    {
        $data = Db::table('question')
            ->alias('q')
            ->join('user u','q.uid = u.uid')
            ->join('cate c','q.cid = c.cid')
            ->field('u.uid,u.nickname,u.face,u.role,q.qid,q.title,c.name,q.ctime,q.kiss,q.answer_num')
            ->order('qid desc')
            ->where('is_top','0')
            ->paginate(5);
        return $data;
    }
    public function getTop()
    {
        $data = Db::table('question')
            ->alias('q')
            ->join('user u','q.uid = u.uid')
            ->join('cate c','q.cid = c.cid')
            ->field('u.uid,u.nickname,u.face,u.role,q.qid,q.title,c.name,q.ctime,q.kiss,q.answer_num')
            ->order('qid desc')
            ->where('is_top','1')
            ->select();
        return $data;
    }
    public function getlist($where)
    {
        $data = Db::table('question')
            ->alias('q')
            ->join('user u','q.uid = u.uid')
            ->join('cate c','q.cid = c.cid')
            ->field('u.uid,u.nickname,u.face,u.role,q.qid,q.title,c.cid,c.name,q.ctime,q.kiss,q.answer_num')
            ->order('qid desc')
            ->where($where)
            ->paginate(5);
        return $data;
    }
    public function getHot()
    {
        $data = Db::table('question')
            ->alias('q')
            ->field('q.qid,q.title,q.answer_num')
            ->where('q.cid',input('cid'))
            ->order('answer_num desc')
            ->where('q.ctime','>',time()-604800)
            ->limit(15)
            ->select();
        return $data;
    }
    public function getwenxin()
    {
        $data = db('question')
        ->where('wenxin',1)
        ->order('qid desc')
        ->limit(5)
        ->select();
        return $data;
    }
}